﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

namespace AspNetSimple
{

    public enum OLECMDF
    {
        // Fields
        OLECMDF_DEFHIDEONCTXTMENU = 0x20,
        OLECMDF_ENABLED = 2,
        OLECMDF_INVISIBLE = 0x10,
        OLECMDF_LATCHED = 4,
        OLECMDF_NINCHED = 8,
        OLECMDF_SUPPORTED = 1
    }

    public enum OLECMDID
    {
        // Fields
        OLECMDID_PAGESETUP = 8,
        OLECMDID_PRINT = 6,
        OLECMDID_PRINTPREVIEW = 7,
        OLECMDID_PROPERTIES = 10,
        OLECMDID_SAVEAS = 4,
        OLECMDID_SHOWSCRIPTERROR = 40
    }
    public enum OLECMDEXECOPT
    {
        // Fields
        OLECMDEXECOPT_DODEFAULT = 0,
        OLECMDEXECOPT_DONTPROMPTUSER = 2,
        OLECMDEXECOPT_PROMPTUSER = 1,
        OLECMDEXECOPT_SHOWHELP = 3
    }

    [ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("B722BCCB-4E68-101B-A2BC-00AA00404770"), ComVisible(true)]
    public interface IOleCommandTarget
    {
        [return: MarshalAs(UnmanagedType.I4)]
        [PreserveSig]
        int QueryStatus(ref Guid pguidCmdGroup, int cCmds, [In, Out] OLECMD prgCmds, [In, Out] IntPtr pCmdText);
        [return: MarshalAs(UnmanagedType.I4)]
        [PreserveSig]
        int Exec(ref Guid pguidCmdGroup, int nCmdID, int nCmdexecopt, [In, MarshalAs(UnmanagedType.LPArray)] object[] pvaIn, ref int pvaOut);
    }

    [StructLayout(LayoutKind.Sequential)]
    public class OLECMD
    {
        [MarshalAs(UnmanagedType.U4)]
        public int cmdID;
        [MarshalAs(UnmanagedType.U4)]
        public int cmdf;
        public OLECMD()
        {
        }
    }
    public static class NativeMethods
    {
        public const int S_FALSE = 1;
        public const int S_OK = 0;

        public static readonly Guid CGID_DocHostCommandHandler = new Guid("f38bc242-b950-11d1-8918-00c04fc2c836");

        public const int VARIANT_TRUE = -1;
        public const int VARIANT_FALSE = 0;
        public const int OLECMDERR_E_NOTSUPPORTED = -2147221244;


    }

    

    public class MyWebBrowser : WebBrowser
    {
        class MyWebBrowserSiteBase : WebBrowserSite, IOleCommandTarget
        {
            private MyWebBrowser _browser;
            public MyWebBrowserSiteBase(MyWebBrowser browser)
                   : base(browser)
            {
                _browser = browser;
            }
            #region IOleCommandTarget Members

            int IOleCommandTarget.QueryStatus(ref Guid pguidCmdGroup, int cCmds, OLECMD prgCmds, IntPtr pCmdText)
            {
                return NativeMethods.S_FALSE;
            }

            int IOleCommandTarget.Exec(ref Guid pguidCmdGroup, int nCmdID, int nCmdexecopt, object[] pvaIn, ref int pvaOut)
            {
                int hResult = NativeMethods.S_OK;
                if (pguidCmdGroup == null)
                    return hResult;
                // Check for invalid pointers (or get a NullReferenceException on a value type???)
                //if (NativeMethods.CGID_DocHostCommandHandler.Equals(pguidCmdGroup))
                {
                    switch (nCmdID)
                    {
                        case (int)OLECMDID.OLECMDID_SHOWSCRIPTERROR:
                            // Hide the dialog
                            pvaOut = NativeMethods.VARIANT_TRUE;


                            break;
                        default:
                            hResult = NativeMethods.OLECMDERR_E_NOTSUPPORTED;
                            break;
                    }
                }
                return hResult;
            }

            #endregion

        }
        private SHDocVw.WebBrowser browser;
        protected override void AttachInterfaces(object nativeActiveXObject)
        {
            browser = (SHDocVw.WebBrowser)nativeActiveXObject;
            browser.Silent = true;
            base.AttachInterfaces(nativeActiveXObject);
        }

        protected override void DetachInterfaces()
        {
            browser = null;
            base.DetachInterfaces();
        }

        protected override WebBrowserSiteBase CreateWebBrowserSiteBase()
        {
            return new MyWebBrowserSiteBase(this);
        }
    }
}
